<!DOCTYPE HTML>
<html class="x-admin-sm">
<head>
    <th:block th:insert="head/head :: common_head"></th:block>
    <style>
        #xybjtp {
            max-width: 100%; /* 限制图片最大宽度为父元素宽度 */
            max-height: 100%; /* 限制图片最大高度为父元素高度 */
            height: auto; /* 保持图片高度按比例缩放 */
            width: auto; /* 保持图片宽度按比例缩放 */
            object-fit: contain; /* 使图片完整显示在容器内，保持比例 */
        }
    </style>
</head>
<body onload="init('xmmc')">
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card" style="margin: 10px;">
                    <div class="layui-tab layui-tab-brief">
                        <div class="layui-tab-content" >
                            <div class="layui-tab-item layui-show">
                                <form class="layui-form layui-form-pane" autocomplete="off">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>项目名称：
                                        </label>
                                        <div class="layui-input-block">
                                            <input id="xmmc" name="xmmc" type="text" class="layui-input" onkeydown="on_key_tab('xsmc',event);">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>项目展示页显示名称：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" class="layui-input" id="xsmc" name="xsmc">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            展示页背景图片：
                                        </label>
                                        <div class="layui-input-block">
                                            <button type="button" class="layui-btn layui-btn-normal"  id="uploadFile">选择图片</button>
                                            <span id="filename" ></span>
                                        </div>
                                    </div>
                                    <div class="layui-form-item" id="xmbj_div" style="display: none">
                                        <label class="layui-form-label">
                                            现有的背景图片：
                                        </label>
                                        <div class="layui-input-block" style="height:200px;">
                                            <image src="" id="xybjtp"></image>
                                        </div>
                                    </div>
                                </form>
                                <div class="layui-form-item">
                                    <button class="layui-btn" lay-submit="" lay-filter="*" id="bc">
                                        新增存盘
                                    </button>
                                    <button type="button" class="layui-btn layui-btn-primary" onclick="window.location.href='query'">返回上页</button>
                                </div>
                                <div style="height:100px;"></div>
                            </div>

                        </div>
                    </div>
                </div>
        </div>
    </div>
</div>
</div>
<th:block th:insert="head/head :: common_jsfiles"></th:block>
<script>
    /**
     * lx 0 表示新增  1 表示修改 id 为主键
     * 所有增改页面，载入时，都是通过doFirst回调renderForm
     *
     */
    var lx="[[${lx}]]";
    var id="[[${id}]]";
    var url = "[[${serverUrl}]]/stamp/xm/";
    //图片显示
    var url1 = "[[${serverUrl}]]/stamp/tp/";
    var ms="stamp_xm_info";
    $(document).ready(function() {
        if(lx=="1"){
            $("#bc").html("修改存盘");
        }else{
            $("#bc").html("新增存盘")
        }
    });
    function on_result(result)
    {
        $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
        if (result.code!=500)
        {
            if (result.code==1 || result.code==2 || result.code==0){
                //新增成功
                layer.msg(result.msg, { time: 1000 }, function() {
                    window.location.href='query'
                });//失败时只提示信息，不关闭弹出层且不刷新父页
            } else{
                layer.msg(result.msg);
            }
        }
        else
        {
            layer.msg(result.msg);
        }
    }
    //表单相关
    layui.use(['form'], function(){
        var form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;
        //文件上传
        var attachmentArray = [];
        var UPLOAD_FILES;
        upload.render({
            elem: '#uploadFile', //绑定元素（上传文件的id）
            url: '', //上传接口
            method: 'POST',
            auto: false,
            accept: 'images',//指定允许上传时校验的文件类型
            field:'wj',//设定文件域的字段名
            size: 1024*4,//限制文件大小4M
            choose:function(obj){
                obj.preview(function(index, file, result){
                    attachmentArray = [];
                    attachmentArray.push(file);
                    $("#filename").html(file.name);
                });
            },
            done: function(res, index, upload) {
            },
            error: function() {//请求异常回调
                layer.closeAll('loading');
                layer.alert('网络异常，请稍后重试！');
            }
        });

        //渲染表格参数
        var renderForm = function (formData){

            if (lx==1){
                $("#xmmc").val(formData.stampXm.name);
                $("#xsmc").val(formData.stampXm.xsmc);
                $("#xmbj_div").show();
                $("#xybjtp").attr("src",url1+"xmbj?id="+formData.stampXm.urlId);
            }
            form.render(); // 重新渲染
        }
        function doFirst()
        {
            //发送的请求
            var jsonData = {
                id: id,
                lx: lx,
                timestamp:new Date().getTime()
            };
            var sendStr = {
                data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
            };
            post_json_form(url+"info",JSON.stringify(sendStr),"数据加载中...",renderForm);
        }

        function onSave(nonce_zhi){
            //获取提交令牌
            if (is_null(nonce_zhi)){
                $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
                layer.msg("没有获取到操作凭证！请重试！",{icon:2,anim:6});
                return false;
            }
            var jsonData = {
                xmmc: $("#xmmc").val(),
                xsmc: $("#xsmc").val(),
                lx: lx,
                id: id,
                nonce_ms:ms,
                nonce_zhi:nonce_zhi,
                timestamp:new Date().getTime()
            };
            var sendStr = {
                data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
            };
            var uploadData = new FormData();
            // //单个选择
            for (var i = 0; i < attachmentArray.length; i++) {
                uploadData.append("wj", attachmentArray[i]);
            }
            uploadData.append('data',encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b")));
            post_data_file_token(url+"save_file",uploadData,"数据提交中...");
        }

        //监听提交
        form.on('submit(*)', function(data){
            if ($("#xmmc").val()==""){
                layer.msg("请输入项目名称！",{icon:2,anim:6});
                return false;
            }
            if (!confirm("是否确定提交存盘？")) return false;
            $('#bc').addClass("layui-btn-disabled").attr("disabled",true);
            psot_get_nonce(ms,"[[${serverUrl}]]/getnonce",onSave)
            return false;
        });

        doFirst();
    });

</script>
</body>
</html>